<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Tamember extends CI_Controller {
	
	function __construct() {
		parent::__construct ();
	
	}
	
	public function index() {
		
		
		
		$id = $this->input->get("id");
		if(!isset($id) || $id == ''){
			$this->load->view('error');
			return ;
		}
		$member = $this->session->userdata ( 'member' );
		/*if($member != null && $member['id'] == $id){
			redirect('member/main');
		}*/
		
		//该会员的信息
		$this->db->select('*');
		$this->db->from('member');
		$this->db->where('id',$id);
		$eData['tamember'] = $this->db->get()->row_array();
		
		//该会员下小店的评论
		$this->db->select('comment.*,store.name,store.photo');
		$this->db->from('comment');
		$this->db->join('store', 'store.id = comment.belongId');
		$this->db->where('store.manager',$id);
		$this->db->where('comment.type',2);
		$this->db->order_by('comment.createtime', 'DESC');
		$this->db->limit(3,0);
		$eData['commentList'] = $this->db->get()->result_array();
		
		//该会员下街道的话题
		$this->db->select('topic.*,street.name,street.logo');
		$this->db->from('topic');
		$this->db->join('street', 'street.id = topic.belongId');
		$this->db->where('street.manager',$id);
		$this->db->where('topic.type',1);
		$this->db->order_by('topic.createtime', 'DESC');
		$this->db->limit(4,0);
		$eData['topicList'] = $this->db->get()->result_array();
		
		//添加的小店
		$this->db->select('store.*');
		$this->db->from('store');
		$this->db->where('store.createid',$id);	
		$this->db->limit(5,0);	
		$eData['addStoreList'] = $this->db->get()->result_array();
		
		$this->db->where('createid',$id);	
		$eData['addStoreCount'] = $this->db->count_all_results('store');
		
		//收藏的小店
		$this->db->select('store.*');
		$this->db->from('store');
		$this->db->join('favorite',"store.id=favorite.favoriteId");	
		$this->db->join('member','member.id=favorite.member');
		$this->db->where('favorite.favoriteType',2);
		$this->db->where('favorite.member',$id);
		$this->db->limit(5,0);	
		$eData['favorStoreList'] = $this->db->get()->result_array();
		
		$this->db->where('favoriteType',2);
		$this->db->where('member',$id);
		$eData['favorStoreCount'] = $this->db->count_all_results('favorite');
		
		//添加的街道
		$this->db->where('createid',$id);	
		$eData['addStreetList'] = $this->db->get('street')->result_array();
		$this->db->where('createid',$id);	
		$eData['addStreetCount'] = $this->db->count_all_results('street');
		//居住的街道
		$this->db->select('street.*');
		$this->db->from('street');
		$this->db->join('livestreet', 'street.id = livestreet.street');
		$this->db->where('livestreet.member',$id);
		$this->db->order_by('street.createtime', 'DESC');
		$this->db->limit(5,0);
		$eData['liveStreetList'] = $this->db->get()->result_array();
		
		$this->db->select('count(*)');
		$this->db->from('street');
		$this->db->join('livestreet', 'street.id = livestreet.street');
		$this->db->where('livestreet.member',$id);
		$eData['liveStreetListCount'] = $this->db->count_all_results();
		
		//相册
		$this->db->where('member',$id);
		$this->db->order_by('modifytime', 'DESC');
		$this->db->limit(3,0);
		$eData['albumList'] = $this->db->get('album')->result_array();
		//他管理的店
		$this->db->where('manager',$id);
		$this->db->order_by('createtime', 'DESC');
		$this->db->limit(4,0);
		$eData['manageStoreList'] = $this->db->get('store')->result_array();
		
		//他关注的人
		$this->db->select('follow.*,member.avatar');
		$this->db->from('follow');
		$this->db->join('member', 'member.id = follow.fmember');
		$this->db->where ('member', $id );
		$this->db->order_by("createtime","DESC");
		$this->db->limit(6,0);
		$followList = $this->db->get()->result_array ();
		$eData ['followList'] = $followList;
		
		//关注他的人
		$this->db->select('follow.*,member.avatar');
		$this->db->from('follow');
		$this->db->join('member', 'member.id = follow.member');
		$this->db->where ('fmember', $id );
		$this->db->order_by("createtime","DESC");
		$this->db->limit(6,0);
		$followList = $this->db->get()->result_array ();
		$eData ['fansList'] = $followList;
		
		//谁看过我
		$this->db->select('visit.*,member.avatar,member.username');
		$this->db->from('visit');
		$this->db->join('member', 'member.id = visit.visitMember');
		$this->db->where('visit.visitId', $id);
		$this->db->order_by("visitTime","DESC");
		$this->db->limit(6,0);
		$visitList = $this->db->get()->result_array ();
		$eData ['visitList'] = $visitList;
		//收藏的		
		
		//访问记录
		
		if($member != null){
			$this->db->where ('visitId', $id );
			$this->db->where ('visitMember', $member['id'] );
			$this->db->delete('visit');
			
			$visit['visitId'] = $id;
			$visit['visitMember'] = $member['id'];
			$visit['visitTime'] = time();
			$this->db->insert('visit',$visit);
		}
		
		$this->load->view('tamember',$eData);
	}
	
}

